import { createRouter, createWebHistory } from 'vue-router';

import InitView from '@/views/InitView.vue';
import TaskView from '@/views/TaskView.vue';
import TaskDetailView from '@/views/TaskDetailView.vue';
import TaskExecuteView from '@/views/TaskExecuteView.vue';
import SettingsView from '@/views/SettingsView.vue';
import FlawStatisticView from '@/views/FlawStatisticView.vue';

const routes = [
  {
    path: '/',
    name: 'Init',
    component: InitView,
    meta: { title: '系统初始化' }
  },
  {
    path: '/tasks',
    name: 'TaskList',
    component: TaskView,
    meta: { title: '任务列表' }
  },
  {
    path: '/tasks/execute/:id',
    name: 'TaskExecute',
    component: TaskExecuteView,
    props: true,
    meta: { title: '任务巡视' }
  },
  {
    path: '/tasks/detail/:id',
    name: 'TaskDetail',
    component: TaskDetailView,
    props: true,
    meta: { title: '任务详情' }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: SettingsView,
    meta: { title: '系统设置' }
  },
  {
    path: '/statistics',
    name: 'FlawStatistics',
    component: FlawStatisticView,
    meta: { title: '缺陷统计' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    redirect: '/',
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = `${to.meta.title} - AGV智能巡检系统`;
  } else {
    document.title = 'AGV智能巡检系统';
  }
  next();
});

export default router;